//
//  PhoneInsideStoreCount.m
//  baobaotong
//
//  Created by 李康 on 2017/4/6.
//  Copyright © 2017年 zzy. All rights reserved.
//

#import "PhoneInsideStoreCount.h"
#import <sys/param.h>
#include <sys/mount.h>
@implementation PhoneInsideStoreCount
+(NSString *)getContentLastSize
{    
    float totalsize = 0.0;
    /// 剩余大小
    float freesize = 0.0;
    /// 是否登录
    NSError *error = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
    if (dictionary)
    {
        NSNumber *_free = [dictionary objectForKey:NSFileSystemFreeSize];
        freesize = [_free unsignedLongLongValue]*1.0;
        
        NSNumber *_total = [dictionary objectForKey:NSFileSystemSize];
        totalsize = [_total unsignedLongLongValue]*1.0;
    }
    return [self getFileSizeString:freesize];
}

+(NSString *)getContentAllSize
{
    struct statfs buf1;
    long long maxspace = 0;
    if (statfs("/", &buf1) >= 0) {
        maxspace = (long long)buf1.f_bsize * buf1.f_blocks;
    }
    if (statfs("/private/var", &buf1) >= 0) {
        maxspace += (long long)buf1.f_bsize * buf1.f_blocks;
    }
    return [self getFileSizeString:maxspace];
}

+(NSString *)getFileSizeString:(CGFloat)size
{
    if (size>1024.0*1024.0*1024.0){
        return [NSString stringWithFormat:@"%.2fG",size/1024.0/1024.0/1024.0];//大于1G，则转化成G单位的字符串
    }
    else if(size<1024.0*1024.0*1024.0&&size>=1024.0*1024.0)//大于1M，则转化成M单位的字符串
    {
        return [NSString stringWithFormat:@"%.2fM",size/1024.0/1024.0];
    }
    else if(size>=1024.0&&size<1024.0*1024.0) //不到1M,但是超过了1KB，则转化成KB单位
    {
        return [NSString stringWithFormat:@"%.2fK",size/1024.0];
    }
    else//剩下的都是小于1K的，则转化成B单位
    {
        return [NSString stringWithFormat:@"%.2fB",size];
    }
}
@end
